/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.home.fragment.home;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.activity.webview.BrowserActivity;
import com.dataoke.shoppingguide.app773026.bean.haodanku.HaodankuIndexTileLongBean;
import com.dataoke.shoppingguide.app773026.util.GlideUtils;
import com.dataoke.shoppingguide.app773026.util.HaoDanKuUtil;

import java.util.List;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class ActViewHolder extends RecyclerView.ViewHolder{

    private Context mContext;
    private ViewPager act_viewpager;

    public ActViewHolder(Context mContext, View itemView) {
        super(itemView);
        this.mContext = mContext;
        act_viewpager = itemView.findViewById(R.id.act_viewpager);
    }

    public void setData(final List<HaodankuIndexTileLongBean> acts) {
        //设置间距
        act_viewpager.setPageMargin(20);
        act_viewpager.setOffscreenPageLimit(3);//>=3
        //setPageTransformer 决定动画效果
//            act_viewpager.setPageTransformer(true, new ScaleInTransformer());
        //有数据后，就设置数据适配器
        act_viewpager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return acts.size();
            }

            /**
             *
             * @param view 页面
             * @param object instantiateItem方法返回的值
             * @return
             */
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            /**
             *
             * @param container ViewPager
             * @param position 对应页面的位置
             * @return
             */
            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                //实例化ImageView
                ImageView imageView = new ImageView(mContext);
                //设置ImageView的拉伸
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                //根据Glide设置图片
//                Glide.with(mContext).load(act_info.get(position).getImg()).into(imageView);
                HaodankuIndexTileLongBean bean = acts.get(position);
                GlideUtils.loadNavCrop(mContext, bean.getImg(), imageView);
                //添加到容器中
                container.addView(imageView);

                //设置点击事件
                imageView.setOnClickListener(view -> {
                    BrowserActivity.show(mContext, HaoDanKuUtil.getUrl(bean),bean.getTitle());

                });
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
    }
}


